"""
@Time ： 2023/7/15 0009 10:07
@Auth ： zpt
@File ：BuildPage.py
@IDE ：PyCharm
"""
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from Page.LoginPage import LoginPage
from selenium.webdriver.common.keys import Keys
from Lib.common_lib import LOG_INFO, LOG_ERROR


class build_information(object):
    """镜像定制页面"""

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def enter_build_page(self):
        """
         进入build 页签
        """
        try:
            self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[2]/div[2]').click()

        except Exception as e:
            message = 'build tab 按钮定位失败'
            LOG_ERROR("error: found build tab button timeout")
            print(e)
            LOG_INFO(message)

    def build_overview_info(self):
        """
        build 页签页面信息检查
        """
        try:
            build_history = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                               '3]/section/section/aside/h2').text
            build_id = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                          '3]/section/section/aside/div[1]/div/div[1]/div['
                                                          '2]/table/thead/tr/th[1]/div').text
            os_variant = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                            '3]/section/section/aside/div[1]/div/div[1]/div['
                                                            '2]/table/thead/tr/th[2]/div').text
            arch = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                      '3]/section/section/aside/div[1]/div/div[1]/div['
                                                      '2]/table/thead/tr/th[3]/div').text
            build_type = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                            '3]/section/section/aside/div[1]/div/div[1]/div['
                                                            '2]/table/thead/tr/th[4]/div').text
            created_time = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                              '3]/section/section/aside/div[1]/div/div[1]/div['
                                                              '2]/table/thead/tr/th[5]/div').text
            status = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                        '3]/section/section/aside/div[1]/div/div[1]/div['
                                                        '2]/table/thead/tr/th[6]/div').text
            opeartion = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                           '3]/section/section/aside/div[1]/div/div[1]/div['
                                                           '2]/table/thead/tr/th[7]/div').text
            details = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                         '3]/section/section/main/div[1]/span').text

            return [build_history, build_id, os_variant, arch, build_type, created_time, status, opeartion, details]

        except Exception as e:
            message = 'build 页签元素定位失败'
            LOG_ERROR("error: found build view information timeout")
            print(e)
            LOG_INFO(message)

    def operation_info(self):
        """
        operation列信息
        """
        try:
            operation = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                           '3]/section/section/aside/div[1]/div/div[1]/div['
                                                           '3]/div/div[1]/div/table/tbody/tr/td['
                                                           '8]/div/button/span').text
            return operation
        except Exception as e:
            message = 'opeartion列信息定位失败'
            LOG_ERROR("error: found  opeartion info button timeout")
            print(e)
            LOG_INFO(message)

    def buid_id_button(self):
        """
        operation列信息
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                               '3]/section/section/aside/div[1]/div/div[1]/div[3]/div/div['
                                               '1]/div/table/tbody/tr/td[1]').click()
        except Exception as e:
            message = 'build tab 按钮定位失败'
            LOG_ERROR("error: found build tab button timeout")
            print(e)
            LOG_INFO(message)

    def build_status(self):
        """
        构建详情中的构建状态
        """
        try:
            status = self.driver.find_element(By.XPATH,
                                              '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                              '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[3]/div/span').text
            return status
        except Exception as e:
            message = '构建状态定位失败'
            LOG_ERROR("error: found build status timeout")
            print(e)
            LOG_INFO(message)

    def enter_package_build_history(self):
        """
        右侧构建详情中单个软件报构建状态查询
        """
        try:
            self.driver.find_element(By.XPATH,
                                     '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                     '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[1]/div/span').click()
            self.driver.implicitly_wait(30)
            operation_status = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                                  '3]/div/div/div[1]/div/div[1]/div[3]/div/div['
                                                                  '1]/div/table/tbody/tr[1]/td[11]/div/span').text

            return operation_status
        except Exception as e:
            message = '详情列表中软件包定位失败'
            LOG_ERROR("error: found build status timeout")
            print(e)
            LOG_INFO(message)

    def details_select_spec(self):
        """
        右侧构建详情查找spec
        """
        try:
            spec_input = self.driver.find_element(By.XPATH, '//main/div[2]/div[2][@class="search-input"]/div/div/input')

            spec_input.send_keys("python-flask")
            time.sleep(2)
            spec_name = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                           '3]/section/section/main/div[3]/div/div[1]/div[3]/div/div['
                                                           '1]/div/table/tbody/tr/td[1]/div/span').text
            return spec_name
        except Exception as e:
            message = '详情列表中软件包定位失败'
            LOG_ERROR("error: found build status timeout")
            print(e)
            LOG_INFO(message)

    def details_select_nonexistent_spec(self):
        """
        右侧构建详情查找spec
        """

        spec_input = self.driver.find_element(By.XPATH, '//main/div[2]/div[2][@class="search-input"]/div/div/input')

        spec_input.send_keys("aaaaa")
        time.sleep(2)
        no_data = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                     '3]/section/section/main/div[3]/div/div[1]/div[3]/div/div['
                                                     '1]/div/div/span').text

        return no_data

    def check_build_details_info(self):
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                               '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[1]/div/span')
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                               '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[2]/div')
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                               '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[3]/div/span')
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div/div[3]/section/section/main/div['
                                               '3]/div/div[1]/div[3]/div/div[1]/div/table/tbody/tr/td[4]/div/span')
        except Exception as e:
            info = "build details中单个软件包信息未正常显示"
            return info
        else:
            info = "build details中单个软件包信息显示正常"
            return info


    def test_build_detail_page_format(self):
        try:
            select_box = self.driver.find_element(By.XPATH,
                                                  '//*[@id="app"]/div[1]/div/div[2]/div/div['
                                                  '3]/section/section/main/div[4]/div/span['
                                                  '2]/div/div/div/div/span/span/i')
            page10 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[1]/span')
            page20 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[2]/span')
            page50 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[3]/span')
            page100 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div[4]/div/div/div[1]/ul/li[4]/span')
            input_text = self.driver.find_element(By.XPATH, '//div/section/section/main/div[4]/div/span['
                                                            '2]/div/div/div/div/input')

            select_box.click()
            time.sleep(1)
            page10.click()
            time.sleep(1)
            infor1 = input_text.get_attribute('value')
            select_box.click()
            time.sleep(1)
            page20.click()
            time.sleep(1)
            infor2 = input_text.get_attribute('value')
            select_box.click()
            time.sleep(1)
            page50.click()
            time.sleep(1)
            infor3 = input_text.get_attribute('value')
            select_box.click()
            time.sleep(1)
            page100.click()
            time.sleep(1)
            infor4 = input_text.get_attribute('value')

            return [infor1, infor2, infor3, infor4]
        except Exception as e:
            message = '构建工程查看按钮定位失败'
            LOG_ERROR("error: found build_project timeout")
            print(e)
            LOG_INFO(message)